package com.colorlight.cloud.xm.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.xm.entity.XmEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author Demon
 * @description 执行结果
 * @date 6/7/2023 上午9:23
 */
@Getter
public enum ResultType implements EnumTrans<XmEntity.ResultType, ResultType> {
	/**
	 * 默认属性
	 * 成功
	 * 失败
	 */
	NOT_DEFINE(XmEntity.ResultType.NOT_DEFINE),
	RESULT_SUCCESS(XmEntity.ResultType.RESULT_SUCCESS),
	RESULT_FAILED(XmEntity.ResultType.RESULT_FAILED),
	;
	private final Integer id;
	private final String name;
	private final XmEntity.ResultType resultType;
	private static final long serialVersionUID = 987654321987654321L;
	
	ResultType(XmEntity.ResultType resultType) {
		this.id = resultType.getNumber();
		this.name = resultType.name();
		this.resultType = resultType;
	}
	
	/**
	 * @param resultType
	 * @return 对应的类型
	 */
	public static ResultType getScheduleTypeStruct(XmEntity.ResultType resultType) {
		Objects.requireNonNull(resultType, "resultType can not be null.");
		return Arrays.stream(ResultType.values())
				.filter(e -> e.name.equals(resultType.name()))
				.findFirst()
				.orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
	}
	
	@Override
	public XmEntity.ResultType toProto() {
		return this.resultType;
	}
	
	@Override
	public ResultType formProto(XmEntity.ResultType resultType) {
		return getScheduleTypeStruct(resultType);
	}
	
	
}
